package com.sigmazero13.gameutils.utils;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sigmazero13.gameutils.R;
import com.sigmazero13.gameutils.model.GamePlayerInfo;

public class PlayInfoPlayerAdapter extends ArrayAdapter<GamePlayerInfo> {
	private Context mContext;
	
	public PlayInfoPlayerAdapter(Context context, int textViewResourceId, List<GamePlayerInfo> players) {
		super(context, textViewResourceId, players);
		mContext = context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.play_info_player, null, false);
		}
		GamePlayerInfo player = getItem(position);
		if (player != null) {
			TextView startPosView = (TextView)v.findViewById(R.id.play_info_player_start);
			Integer startPos = player.getStartingPos();
			if (startPos == null || startPos == 0) {
				startPosView.setVisibility(View.GONE);
			} else {
				startPosView.setText(startPos.toString() + ")");
				startPosView.setVisibility(View.VISIBLE);
			}
			
			TextView playerName = (TextView)v.findViewById(R.id.play_info_player_name);
			playerName.setText(player.getName());
			
			TextView userNameView = (TextView)v.findViewById(R.id.play_info_player_username);
			String userName = player.getUsername();
			if (userName == null || userName.equals("")) {
				userNameView.setVisibility(View.GONE);
			} else {
				userNameView.setText("(" + userName + ")");
				userNameView.setVisibility(View.VISIBLE);
			}
			
			View teamRow = v.findViewById(R.id.play_info_player_team_row);
			String playerTeam = player.getTeam();
			if (playerTeam == null || playerTeam.equals("")) {
				teamRow.setVisibility(View.GONE);
			} else {
				TextView teamView = (TextView)v.findViewById(R.id.play_info_player_team);
				teamView.setText(playerTeam);
				teamRow.setVisibility(View.VISIBLE);
			}
			
			View scoreRow = v.findViewById(R.id.play_info_player_score_row);
			Integer playerScore = player.getScore();
			if (playerScore == null) {
				scoreRow.setVisibility(View.GONE);
			} else {
				TextView scoreView = (TextView)v.findViewById(R.id.play_info_player_score);
				scoreView.setText(playerScore.toString());
				scoreRow.setVisibility(View.VISIBLE);
			}
			
			View winnerView = v.findViewById(R.id.play_info_player_winner);
			if (player.getWinner()) {
				v.setBackgroundResource(R.color.win_bg_color);
				winnerView.setVisibility(View.VISIBLE);
			} else {
				v.setBackgroundResource(R.color.lose_bg_color);
				winnerView.setVisibility(View.GONE);
			}
		}
		
		return v;
	}
}
